package com.castle.fortress.admin.member.loginLog.service.impl;

import cn.hutool.core.util.StrUtil;

import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.member.loginLog.entity.MemberLoginLogEntity;
import com.castle.fortress.admin.member.loginLog.dto.MemberLoginLogDto;
import com.castle.fortress.admin.member.loginLog.mapper.MemberLoginLogMapper;
import com.castle.fortress.admin.member.loginLog.service.MemberLoginLogService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;

/**
 * 会员登录日志表 服务实现类
 *
 * @author 
 * @since 2025-04-15
 */
@Service
public class MemberLoginLogServiceImpl extends ServiceImpl<MemberLoginLogMapper, MemberLoginLogEntity> implements MemberLoginLogService {
    /**
    * 获取查询条件
    * @param memberLoginLogDto
    * @return
    */
    public QueryWrapper<MemberLoginLogEntity> getWrapper(MemberLoginLogDto memberLoginLogDto){
        QueryWrapper<MemberLoginLogEntity> wrapper= new QueryWrapper();
        if(memberLoginLogDto != null){
            MemberLoginLogEntity memberLoginLogEntity = ConvertUtil.transformObj(memberLoginLogDto,MemberLoginLogEntity.class);
            wrapper.like(memberLoginLogEntity.getId() != null,"id",memberLoginLogEntity.getId());
            wrapper.like(memberLoginLogEntity.getMemberId() != null,"member_id",memberLoginLogEntity.getMemberId());
            wrapper.like(StrUtil.isNotEmpty(memberLoginLogEntity.getInvokeUrl()),"invoke_url",memberLoginLogEntity.getInvokeUrl());
            wrapper.like(StrUtil.isNotEmpty(memberLoginLogEntity.getRemoteAddr()),"remote_addr",memberLoginLogEntity.getRemoteAddr());
            wrapper.like(StrUtil.isNotEmpty(memberLoginLogEntity.getAddress()),"address",memberLoginLogEntity.getAddress());
            wrapper.like(StrUtil.isNotEmpty(memberLoginLogEntity.getCusBrowser()),"cus_browser",memberLoginLogEntity.getCusBrowser());
            wrapper.like(StrUtil.isNotEmpty(memberLoginLogEntity.getCusOs()),"cus_os",memberLoginLogEntity.getCusOs());
            wrapper.like(StrUtil.isNotEmpty(memberLoginLogEntity.getInvokeParams()),"invoke_params",memberLoginLogEntity.getInvokeParams());
            wrapper.like(StrUtil.isNotEmpty(memberLoginLogEntity.getInvokeStatus()),"invoke_status",memberLoginLogEntity.getInvokeStatus());
            wrapper.like(memberLoginLogEntity.getInvokeTime() != null,"invoke_time",memberLoginLogEntity.getInvokeTime());
            wrapper.like(StrUtil.isNotEmpty(memberLoginLogEntity.getResultData()),"result_data",memberLoginLogEntity.getResultData());
            wrapper.like(memberLoginLogEntity.getElapsedTime() != null,"elapsed_time",memberLoginLogEntity.getElapsedTime());
            wrapper.like(memberLoginLogEntity.getCreateTime() != null,"create_time",memberLoginLogEntity.getCreateTime());
        }
        return wrapper;
    }


    @Override
    public IPage<MemberLoginLogDto> pageMemberLoginLog(Page<MemberLoginLogDto> page, MemberLoginLogDto memberLoginLogDto) {
        QueryWrapper<MemberLoginLogEntity> wrapper = getWrapper(memberLoginLogDto);
        Page<MemberLoginLogEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<MemberLoginLogEntity> memberLoginLogPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<MemberLoginLogDto> pageDto = new Page(memberLoginLogPage.getCurrent(), memberLoginLogPage.getSize(),memberLoginLogPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(memberLoginLogPage.getRecords(),MemberLoginLogDto.class));
        return pageDto;
    }


    @Override
    public List<MemberLoginLogDto> listMemberLoginLog(MemberLoginLogDto memberLoginLogDto){
        QueryWrapper<MemberLoginLogEntity> wrapper = getWrapper(memberLoginLogDto);
        List<MemberLoginLogEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,MemberLoginLogDto.class);
    }
}

